package com.example.calender.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.calender.R;
import com.haibin.calendarview.Calendar;

import java.util.List;

public class CalenderAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<Object> listData;
    private Context context;
    private static final int TIME = 0;
    private static final int EVENT = 2;
    private EventListener eventListener;

    public CalenderAdapter(List<Object> listData, Context context) {
        this.listData = listData;
        this.context = context;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater layoutInflater = LayoutInflater.from(context);
        View view;
        switch (viewType){
            case TIME:
                view = layoutInflater.inflate(R.layout.item_layout, parent, false);
                return new TimeViewHolder(view);
            case EVENT:
                view = layoutInflater.inflate(R.layout.item_layout2, parent, false);
                return new EventViewHolder(view);
            default:
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Object o = listData.get(position);
        if(o instanceof String){
            String str = o.toString();
            ((TimeViewHolder)holder).tvTime.setText(str);
        }
        if (o instanceof Calendar.Scheme){
            Calendar.Scheme scheme = (Calendar.Scheme)o;
            ((EventViewHolder)holder).tvEvent.setText(scheme.getScheme());
            ((EventViewHolder)holder).tvTime.setText(scheme.getOther());
            ((EventViewHolder)holder).itemView.setOnClickListener(v -> {
                eventListener.click(scheme.getScheme());
            });
        }
    }


    @Override
    public int getItemCount() {
        return listData==null? 0 :listData.size();
    }

    @Override
    public int getItemViewType(int position) {
        if(listData!=null){
            Object o = listData.get(position);
            if(o instanceof String){
                return TIME;
            }
            if (o instanceof Calendar.Scheme){
                return EVENT;
            }

        }
        return super.getItemViewType(position);
    }

    public class TimeViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvTime;

        public TimeViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tv_time);
        }
    }

    public class EventViewHolder extends RecyclerView.ViewHolder {

        private final TextView tvTime;
        private final TextView tvEvent;

        public EventViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTime = itemView.findViewById(R.id.tv_time);
            tvEvent = itemView.findViewById(R.id.tv_event);
        }
    }

    public void setEventListener(EventListener eventListener){
        this.eventListener=eventListener;
    }

    public interface EventListener{
        void click(String title);
    }
}
